package cn.java83cloud.springcloudclient.web;

import cn.java83cloud.springcloudclient.entity.Advtise;
import cn.java83cloud.springcloudclient.entity.Page;
import cn.java83cloud.springcloudclient.entity.Result;
import cn.java83cloud.springcloudclient.service.AdvService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author oldliu
 * @since 1.0
 */
@RestController
@RequestMapping("advclient")
public class AdvtiseController {
    @Autowired
    private AdvService advService;

    @GetMapping("get/{id}")
    public Result get(@PathVariable("id") Long id){
        return advService.get(id);
    }
    @GetMapping("/list")
    @HystrixCommand(fallbackMethod = "searchError",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60")
    })

    public Page<Advtise> search(@RequestParam(name = "page", defaultValue = "1") int page,
                                @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                @RequestParam(name = "title", required = false) String title,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(name = "start", required =
                                        false) Date start,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(name = "end", required = false) Date end) {
        return advService.search(page, pageSize, title, start, end);
    }
    public Page<Advtise> searchError(@RequestParam(name = "page", defaultValue = "1") int page,
                                @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                @RequestParam(name = "title", required = false) String title,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(name = "start", required =
                                        false) Date start,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(name = "end", required = false) Date end) {
        Page p=new Page();
        p.setCode(429);
        p.setMsg("访问人数太多，请稍后再试");
        return p;
    }
}
